import vue from '@vitejs/plugin-vue'
import path from 'path'
import { build, defineConfig, loadEnv } from 'vite'
// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  let env = loadEnv(mode, process.cwd(), '')
  return {
    plugins: [vue()],

    server: {
      port: 3002,
      proxy: {
        '^/[A-Za-z0-9]{32}.[A-Za-z0-9]+': {
          target: env.VITE_APP_FILE_API,
          changeOrigin: true,
          rewrite: path => path.substring(path.lastIndexOf('/'))
        },
        '^/.*/[A-Za-z0-9]{32}.[A-Za-z0-9]+': {
          target: env.VITE_APP_FILE_API,
          changeOrigin: true,
          rewrite: path => path.substring(path.lastIndexOf('/'))
        },
        '/ddm/upload': {
          target: env.VITE_APP_FILE_API,
          changeOrigin: true,
          rewrite: path => path.replace(/^\/ddm/, '')
        },
        '/ddm': {
          target: env.VITE_APP_BASE_API,
          changeOrigin: true,
          rewrite: path => path.replace(/^\/ddm/, '')
        },
      }
    },
    resolve: {
      // https://cn.vitejs.dev/config/#resolve-alias
      alias: {
        // 设置路径
        '~': path.resolve(__dirname, './'),
        // 设置别名
        '@': path.resolve(__dirname, 'src')
      },
      // https://cn.vitejs.dev/config/#resolve-extensions
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    build: {
      target: ['edge90', 'chrome90', 'firefox90', 'safari15']
    },
    optimizeDeps: {
      include: ['rest']
    }
  }
})
